using System;
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;


namespace iDorm
{
	// Renderer
	//
	// Handles any low level rendering and is the exclusive entry point 
	// to the direct3D library.

	public class Renderer
	{
		private Device device;
		private PresentParameters pp;
		
		// Constructor
		//
		// Description: Creates a standard device and sets parameters to 
		//				default values
		// Input:		Form - where to render the graphics
		// Output:		None
		public Renderer(System.Windows.Forms.Panel target)
		{
			pp = new PresentParameters();
			pp.SwapEffect = SwapEffect.Discard;
			pp.Windowed = true;
			pp.EnableAutoDepthStencil = true;
			pp.AutoDepthStencilFormat = DepthFormat.D24S8;

			try
			{
				device = new Device(0, DeviceType.Hardware, target, 
									CreateFlags.SoftwareVertexProcessing, pp);
			}
			catch (DirectXException)
			{
				MessageBox.Show("Unable to initialize DirectX");
				Application.Exit();
			}

			// Setup device
			device.RenderState.Lighting = true;
			device.Lights[0].Type = LightType.Directional;
			device.Lights[0].Direction = new Vector3(-0.3f, -2, 0);
			device.Lights[0].Diffuse = System.Drawing.Color.White;
			device.Lights[0].Commit();
			device.Lights[0].Enabled = true;

			

			device.RenderState.Ambient = Color.Gray;

		}

		// BeginRenderD
		public void BeginRender()
		{
			device.Clear(ClearFlags.Target, Color.White, 1.0f, 0);
			device.BeginScene();

			// Now the device is prepared to write to the back buffer
		}

		public void EndRender()
		{
			device.EndScene();
			device.Present();
		}


		public Device GetDevice()
		{
			return device;
		}
	}
}
